{extend name="admin/public/base" /}

{block name="content"}
<!-- Content Header (Page header) -->
<section class="content-header">
  <h1>
    书籍列表
    <small></small>
  </h1>
  <ol class="breadcrumb">
    <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
    <li><a href="#">内容</a></li>
    <li class="active">书籍列表</li>
  </ol>
</section>

<!-- Main content -->
<section class="content">

    <div class="box box-solid">
        <div class="box-header with-border">
            <a href="{:Url('admin/book/create')}" class="btn btn-default btn-flat"><i class="fa fa-plus-square"></i> 创建图书 </a>
            <div class="pull-right">
                <form method="get" class="form-inline">
                    <div class="input-group">
                        <select class="form-control" name="category_id">
                            <option value="">选择栏目</option>
                            {volist name="category" id="vo"}
                            <option {notempty name="search.category_id"}{eq name="search.category_id" value="$vo.id"}selected{/eq}{/notempty} value="{$vo.id}">{$vo.html}{$vo.title}{$vo.html}</option>
                            {/volist}
                        </select>
                    </div>
                    <div class="input-group">
                      <input type="text" name="title" value="{$search.title|default=""}" class="form-control" placeholder="Search ...">
                      <span class="input-group-btn">
                        <button type="submit" class="btn btn-default btn-flat">搜索</button>
                      </span>
                    </div>
                </form>
            </div>
        </div>

        <div class="box-body">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th><input id="checkAll" type="checkbox"></th>
                            <th>#ID</th>
                            <th>标题</th>
                            <th>栏目</th>
                            <th>状态</th>
                            <th>审核</th>
                            <th>推荐</th>
                            <th>热门</th>
                            <th>分卷</th>
                            <th>章节</th>
                            <th>日期</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                    {volist name="list" id="vo"}
                    <tr  id = "t_{$vo.id}">
                        <td><input name="ids[]" value="{$vo.id}" type="checkbox"></td>
                        <td>{$vo.id}</td>
                        <td><a target="_blank" href="{:url('index/book/read', ['id' => $vo['id']])}">{$vo.title}</a></td>
                        <td>{$vo.category_name}</td>
                        <td>
                            {eq name="vo.flag" value="1"}
                                完结
                            {else/}
                                连载
                            {/eq}
                        </td>
                        <td>
                            {eq name="vo.status" value="1"}
                            <small class="label bg-light-blue"><i class="fa fa-check"></i></small>
                            {else/}
                            <small class="label bg-gray"><i class="fa fa-times"></i></small>
                            {/eq}
                        </td>
                        <td>
                            {eq name="vo.hot" value="1"}
                            <small class="label bg-light-blue"><i class="fa fa-check"></i></small>
                            {else/}
                            <small class="label bg-gray"><i class="fa fa-times"></i></small>
                            {/eq}
                        </td>
                        <td>
                            {eq name="vo.recommend" value="1"}
                            <small class="label bg-light-blue"><i class="fa fa-check"></i></small>
                            {else/}
                            <small class="label bg-gray"><i class="fa fa-times"></i></small>
                            {/eq}
                        </td>
                        <td><a href="{:url('admin/section/index', ['id' => $vo['id']])}">分卷管理</a></td>
                        <td><a href="{:url('admin/chapter/index', ['id' => $vo['id']])}">章节管理</a></td>
                        <td>{$vo.create_at}</td>
                        <td>
                          <div class="btn-group">
                            <a href="{:url('admin/book/edit',['id'=>$vo.id])}" class="btn btn-default btn-xs"><i class="fa fa-edit"></i></a>
                            <a onClick="deleteOne({$vo.id})" class="btn btn-default btn-xs"><i class="fa fa-trash-o"></i></a>
                          </div>
                        </td>
                    </tr>
                    {/volist}
                    </tbody>
                </table>
            </div>

        </div>
        <div class="box-footer">
            <div class="row">
                <div class="col-sm-6">
                    <form method="post" class="form-inline">
                        <div class="input-group">
                            <div><select id="type" class="form-control">
                                <option value="recommend">推荐</option>
                                <option value="hot">热门</option>
                                <option value="change">审核</option>
                                <option value="cancelrecommend">取消推荐</option>
                                <option value="cancelhot">取消热门</option>
                                <option value="delete">删除</option>
                            </select>
                            </div>
                            <span class="input-group-btn"> 
                                <button type="button" onclick="apply()" class="btn btn-primary btn-flat"><i class="fa fa-save"></i> 应用 </button> 
                            </span>
                        </div>
                    </form>
                </div>
                
                <div class="col-sm-6">
                    <div class="pull-right">
                        {$page}
                    </div>
                </div>
            </div>
        </div>
    </div>

</section>
<!-- /.content -->
{/block}

{block name="js"}
<script>
    function deleteOne(id){
            swal({
                title: "确定删除这条信息吗?",
                text: "",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                closeOnConfirm: false,
                closeOnCancel: false },
            function (isConfirm) {
                if (isConfirm) {
                    $.ajax({
                        url:"{:url('admin/book/remove')}",
                        data:{'id':id},
                        type:'post',
                        dataType:'json',
                        success: function(xhr){
                            if(xhr.code == 200){
                                swal(xhr.msg, "", "success");
                                $("#t_"+id).remove();
                            }else{
                                swal(xhr.msg, "", "error");
                            }
                        }
                    })
                } else {
                    swal("取消操作", "", "error");
                }
            });
    }

    //全选
    $("#checkAll").click(function(){
    //判断当前点击的复选框处于什么状态$(this).is(":checked") 返回的是布尔类型
    if($(this).is(":checked")){
        $("input[name='ids[]']").prop("checked",true);
    }else{
        $("input[name='ids[]']").prop("checked",false);
        }
    });

    //批量操作
    function apply(){
        var chk_value =[]; 
        $('input[name="ids[]"]:checked').each(function(){ 
            chk_value.push($(this).val()); 
        }); 
        var type = $('#type option:selected').val() ;
        if(chk_value.length < 1){
            swal({
                title: "未选择任何记录!",
                text: "",
                type: "error"
            });
        }else{
            swal({
                title: "确定要进行操作吗?",
                text: "",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                closeOnConfirm: false,
                closeOnCancel: false },
                function (isConfirm) {
                    if (isConfirm) {
                        $.ajax({
                            url:"{:url('admin/book/handle')}",
                            data:{'ids':chk_value, 'type':type},
                            type:'post',
                            dataType:'json',
                            success: function(xhr){
                                if(xhr.code == 200){
                                    swal({
                                        title: xhr.msg,
                                        text: "",
                                        type: "success",
                                        confirmButtonText: "确定",
                                        },function (isConfirm) {
                                            if (isConfirm) {
                                                window.location.reload();
                                            }
                                        });
                                }else{
                                    swal({
                                        title: xhr.msg,
                                        text: "",
                                        type: "error",
                                        confirmButtonText: "确定",
                                        });
                                }
                            }
                        })
                    } else {
                        swal("取消操作", "", "error");
                    }
                });
        }
    }

</script>
{/block}
